package cn.itcast.shop.adminuser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import cn.itcast.shop.user.User;
import cn.itcast.shop.user.UserService;
import cn.itcast.shop.utils.PageBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class AdminUserAction extends ActionSupport implements ModelDriven<AdminUser>{
	
	private AdminUser adminUser=new AdminUser();
	
	private AdminUserService adminUserService;
	private Integer page;
	private PageBean<User> pageBean;
	
	private UserService userService;
	
	private String remember="2";
	
	



	public void setRemember(String remember) {
		this.remember = remember;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public PageBean<User> getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean<User> pageBean) {
		this.pageBean = pageBean;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	

	public void setAdminUserService(AdminUserService adminUserService) {
		this.adminUserService = adminUserService;
	}

	@Override
	public AdminUser getModel() {
		// TODO Auto-generated method stub
		return adminUser;
	}
	
	public String login(){  
		
		if(remember.equals("1")){
			ServletActionContext.getRequest().getSession().setAttribute("Re", adminUser);
		}
		
		AdminUser existAdminUser=adminUserService.login(adminUser);
		if(existAdminUser==null){
			this.addActionError("登陆失败！");
			return LOGIN;
		}else{
			ServletActionContext.getRequest().getSession().setAttribute("existAdminUser", existAdminUser);
			return "loginSuccess";
		}
	}
	public String welcome(){
		return "welcome";
	}
	
	public String adminFindAll(){
		pageBean=userService.findByPages(page);
		return "adminFindAllSuccess";
	}
	
	public String quit(){
		ServletActionContext.getRequest().getSession().invalidate();
		return "quitSuccess";
	}
}
